﻿using QDChat;
using QDChat.Api.Models;
using QDChat.Domain;
using QDChat.Service;
using QDChat.Util;
using System;
using System.Collections.Generic;
using WeiTap.Restful;

namespace QDChat.Api.Controllers
{
    public class IMController : WebApi
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public object Login(string openId)
        {
            var user = UserService.GetByOpenId(openId);
            if (user == null)
                throw new Exception("用户不存在");
            return new { Id = user.Id, NickName = user.NickName, Avatar = user.Avatar, Token = "" };
        }
        /// <summary>
        /// 导入用户
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="nickName"></param>
        /// <param name="avatar"></param>
        /// <returns></returns>
        public bool ImportUser(string openId, string nickName, string avatar)
        {
            return UserService.ImportUser(new User()
            {
                OpenId = openId,
                NickName = nickName,
                Avatar = avatar,
                CreateTime = DateTime.Now
            }) > 0;
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public object SendMsg(Message model)
        {
            int result = MsgService.SendMsg(model);
            SessionService.Append(new Session()
            {
                FromUid = model.FromUid,
                ToUid = model.ToUid,
                SendTime = Utils.getTimestamp(),
                LastMsg = model.Msg,
                ToMsgCount = 1
            });
            return new { Id = model.Id, FromUid = model.FromUid, ToUid = model.ToUid, SendTime = model.SendTime };
        }

        /// <summary>
        /// 消息列表
        /// </summary>
        /// <returns></returns>
        public object MsgList(int fromUid, int toUid, int page = 1, int pageSize = 20)
        {
            SessionService.ResetCount(fromUid, toUid);
            return new MsgList()
            {
                ToUser = UserService.Get(toUid),
                FromUser = UserService.Get(fromUid),
                List = MsgService.GetMsgList(fromUid, toUid, page, pageSize)
            };
        }

        /// <summary>
        /// 会话列表
        /// </summary>
        /// <returns></returns>
        public object SessionList(string openId, int page = 1, int pageSize = 20)
        {
            var myUser = UserService.GetByOpenId(openId);
            var list = myUser == null ?
                    new List<SessionView>() :
                    SessionService.GetList(myUser.Id, page, pageSize);

            foreach (var item in list)
            {
                item.MsgCount = item.FromUid == myUser.Id ?
                        item.FromMsgCount :
                        item.ToMsgCount;

                var user = item.FromUid == myUser.Id ?
                         UserService.Get(item.ToUid) :           //我发的消息
                         UserService.Get(item.ToUid);            //我收到的消息

                if (user != null)
                {
                    item.Avatar = user.Avatar;
                    item.NickName = user.NickName;
                }
            }

            return new
            {
                FromUser = myUser,
                List = list
            };
        }

        public bool AddSession(int fromUid, int toUid, string msg)
        {
            return SessionService.Append(new Session()
            {
                FromUid = fromUid,
                ToUid = toUid,
                SendTime = Utils.getTimestamp(),
                LastMsg = msg,
                ToMsgCount = 0
            }) > 0;
        }

        /// <summary>
        /// 好友列表
        /// </summary>
        /// <returns></returns>
        public object FriendList(string fromUid, int page = 1, int pageSize = 20)
        {
            return new List<object>();
        }
    }
}